package com.gk.panda.apis.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gk.panda.apis.Vo.exports.ExportEnvMonitorVO;
import com.gk.panda.apis.dto.EnvListDto;
import com.gk.panda.apis.entity.env.vo.EnvMonitorVO;
import com.gk.panda.apis.entity.env.vo.TrendInfoVO;
import com.gk.panda.apis.entity.env.vo.TrendVO;
import com.gk.panda.apis.entity.largeScreen.Enum.OrgEnum;
import com.gk.panda.apis.enums.TrendEnum;
import com.gk.panda.apis.mapper.DeviceEnvMonitorServiceMapper;
import com.gk.panda.apis.service.DeviceEnvMonitorService;
import com.gk.panda.apis.service.OrgService;
import com.gk.panda.apis.util.BeanDtoVoUtils;
import com.gk.panda.apis.util.ListCopyUtils;
import com.gk.panda.apis.util.excel.ExcelUtils;
import com.gk.panda.commons.constant.Constants;
import com.gk.panda.commons.exception.ServerException;
import com.gk.panda.pojo.DeviceEnvMonitorEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @Description 大华设备环境监测业务处理
 * @Author zhangguoqiao
 * @Date 2022/12/13 15:05
 * @Version 1.0
 */
@Service
public class DeviceEnvMonitorServiceImpl extends ServiceImpl<DeviceEnvMonitorServiceMapper, DeviceEnvMonitorEntity> implements DeviceEnvMonitorService {

    @Autowired
    private OrgService orgService;

    /**
     * 统计列表查询
     *
     * @param dto P
     * @return R
     */
    @Override
    public IPage<EnvMonitorVO> getEnvMonitorlList(EnvListDto dto) {
//        List<Integer> dtoOrgIds = dto.getOrgIds();
//        if (dtoOrgIds == null || dtoOrgIds.size() == 0) dtoOrgIds = Collections.singletonList(OrgEnum.CHENGDU.getOrgId());
//        List<Integer> orgIds = new ArrayList<>();
//        for (Integer dtoOrgId : dtoOrgIds) {
//            List<Integer> puisneOrgIds = orgService.getPuisneOrgIds(dtoOrgId);
//            orgIds.addAll(puisneOrgIds);
//        }
        Page<DeviceEnvMonitorEntity> page = new Page<>(dto.getPageNumber(), dto.getPageSize());
        QueryWrapper<DeviceEnvMonitorEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DeviceEnvMonitorEntity::getDelFlag, Constants.ZERO)
                .in(dto.getOrgIds() !=null,DeviceEnvMonitorEntity::getOrgId,dto.getOrgIds())
                .gt(StringUtils.isNotEmpty(dto.getStartTime()),DeviceEnvMonitorEntity::getCreateTime,dto.getStartTime())
                .lt(StringUtils.isNotEmpty(dto.getEndTime()),DeviceEnvMonitorEntity::getCreateTime,dto.getEndTime())
//                .orderByAsc(DeviceEnvMonitorEntity::getOrgId)
                .orderByDesc(DeviceEnvMonitorEntity::getMonitorTime);
        Page<DeviceEnvMonitorEntity> entityPage = page(page, wrapper);
        return BeanDtoVoUtils.pageVo(entityPage, EnvMonitorVO.class);
    }

    /**
     * 分页导出统计列表
     *
     * @param response   P
     * @param pageNumber P
     * @param pageSize   P
     */
    @Override
    public void exportEnvMonitor(HttpServletResponse response, Integer pageNumber, Integer pageSize) {
        Page<DeviceEnvMonitorEntity> page = new Page<>(pageNumber, pageSize);
        QueryWrapper<DeviceEnvMonitorEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(DeviceEnvMonitorEntity::getDelFlag, Constants.ZERO)
//                .orderByAsc(DeviceEnvMonitorEntity::getOrgId)
                .orderByDesc(DeviceEnvMonitorEntity::getMonitorTime);
        Page<DeviceEnvMonitorEntity> entityPage = page(page, wrapper);
        List<DeviceEnvMonitorEntity> records = entityPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            throw new ServerException("暂无数据导出！");
        }
        List<ExportEnvMonitorVO> vos = ListCopyUtils.copyListProperties(records, ExportEnvMonitorVO::new);
        // 导出
        ExportParams params = new ExportParams();
        params.setSheetName("生态环境检测信息");
        params.setHeight((short) 15);
        Workbook workbook = ExcelExportUtil.exportBigExcel(params, ExportEnvMonitorVO.class, vos);
        ExcelExportUtil.closeExportBigExcel();
        // 生成文件
        String downloadFileName = "生态环境检测统计导出" + ".xlsx";
        ExcelUtils.download(downloadFileName, workbook, response);

    }

    /**
     * 获取过去24小时生态趋势图
     *
     * @param orgId P
     * @return R
     */
    @Override
    public List<TrendVO> getTrendList(Integer orgId) {
        List<Integer> orgIds = orgService.getPuisneOrgIds(orgId);
        List<DeviceEnvMonitorEntity> list = list(Wrappers.<DeviceEnvMonitorEntity>lambdaQuery()
                .eq(DeviceEnvMonitorEntity::getDelFlag, Constants.ZERO)
                .in(DeviceEnvMonitorEntity::getOrgId, orgIds)
                .apply("create_time > date_add(now(), interval -24 hour)"));
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        List<TrendInfoVO> infos1 = new ArrayList<>();
        List<TrendInfoVO> infos2 = new ArrayList<>();
        List<TrendInfoVO> infos3 = new ArrayList<>();
        List<TrendInfoVO> infos4 = new ArrayList<>();
        List<TrendInfoVO> infos5 = new ArrayList<>();
        List<TrendInfoVO> infos6 = new ArrayList<>();
        list.forEach(k -> {
            String monitorTime = k.getMonitorTime();
            String substring = monitorTime.substring(monitorTime.length() - 2, monitorTime.length());
            TrendInfoVO vo1 = new TrendInfoVO();
            TrendInfoVO vo2 = new TrendInfoVO();
            TrendInfoVO vo3 = new TrendInfoVO();
            TrendInfoVO vo4 = new TrendInfoVO();
            TrendInfoVO vo5 = new TrendInfoVO();
            TrendInfoVO vo6 = new TrendInfoVO();
            vo1.setTime(substring);
            vo1.setValue(k.getTemperature());
            infos1.add(vo1);
            vo2.setTime(substring);
            vo2.setValue(k.getHumidity());
            infos2.add(vo2);
            vo3.setTime(substring);
            vo3.setValue(k.getWindSpeed());
            infos3.add(vo3);
            vo4.setTime(substring);
            vo4.setValue(k.getWindDirection());
            infos4.add(vo4);
            vo5.setTime(substring);
            vo5.setValue(k.getPm25());
            infos5.add(vo5);
            vo6.setTime(substring);
            vo6.setValue(k.getPm10());
            infos6.add(vo6);
        });
        TrendVO vo1 = new TrendVO();
        TrendVO vo2 = new TrendVO();
        TrendVO vo3 = new TrendVO();
        TrendVO vo4 = new TrendVO();
        TrendVO vo5 = new TrendVO();
        TrendVO vo6 = new TrendVO();
        vo1.setType(TrendEnum.TEMPERATURE.getName());
        vo1.setTrendInfoVOS(infos1);
        vo2.setType(TrendEnum.HUMIDITY.getName());
        vo2.setTrendInfoVOS(infos2);
        vo3.setType(TrendEnum.WINDSPEED.getName());
        vo3.setTrendInfoVOS(infos3);
        vo4.setType(TrendEnum.WINDDIRECTION.getName());
        vo4.setTrendInfoVOS(infos4);
        vo5.setType(TrendEnum.PM25.getName());
        vo5.setTrendInfoVOS(infos5);
        vo6.setType(TrendEnum.PM10.getName());
        vo6.setTrendInfoVOS(infos6);
        return Arrays.asList(vo1, vo2, vo3, vo4, vo5, vo6);
    }
}
